package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.BuyCarAdapter;
import com.sxm.yiwei.beans.Car;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class BuyCarActivity extends BaseActivity implements View.OnClickListener{
    private String TAG ="Car";
    private SlideListView1 carlist;
    private ImageView carback;
    private SharedPreferences sharedPreferences;
    private String token;
    private String userId;
    private String appId;
    private int no = 1;
    private BuyCarAdapter adapter;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_car);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        userId = sharedPreferences.getString("userId", "");
        appId = sharedPreferences.getString("appid", "");
        getCartList();
        initBuycarView();
    }
        // 获取购物车列表
    private void getCartList() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETCARTLIST);
        params.addHeader("Content-Type","application/json");
        params.addHeader("appId",appId);
        params.addHeader("token",token);
        params.addHeader("userId",userId);
        JSONObject obj = new JSONObject();
        obj.put("pageNo",no+"");
                obj.put("pageSize","20");
                params.addParameter("params",obj.toJSONString());
                x.http().get(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {

                        JSONObject object = JSON.parseObject(result);
                        JSONArray data = object.getJSONArray("data");
                        List<Car.DataBean> list = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            Car.DataBean dataBean = new Car.DataBean();
                            JSONObject obj1 = JSON.parseObject(String.valueOf(data.get(i)));
                            String amount = obj1.getString("amount");
                            String id = obj1.getString("id");
                            String intro = obj1.getString("intro");
                            String name = obj1.getString("name");
                            String serviceAvatar = obj1.getString("serviceAvatar");
                            String unit = obj1.getString("unit");
                            dataBean.setAmount(Double.valueOf(amount));
                            dataBean.setId(id);
                            dataBean.setIntro(intro);
                            dataBean.setName(name);
                            dataBean.setServiceAvatar(serviceAvatar);
                            dataBean.setUnit(Integer.valueOf(unit));
                            list.add(dataBean);
                        }


                        adapter = new BuyCarAdapter(BuyCarActivity.this,list);
                        carlist.setAdapter(adapter);

                        adapter.setCarItemOnClick(new BuyCarAdapter.CarItemOnClick() {
                            @Override
                            public void OnClickTobuy(int p) {
                                Car.DataBean dataBean = adapter.list.get(p);
                                Intent in = new Intent(BuyCarActivity.this,FwBuyServiceActivity.class);
                                in.putExtra("id",dataBean.getId());
                                in.putExtra("price",dataBean.getAmount()+"");
                                startActivity(in);
                            }

                            @Override
                            public void OnClickDelete(int p) {
                                //删除
                                Car.DataBean dataBean = adapter.list.get(p);

                                addCar(dataBean.getId(),p);

                            }
                        });
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                        ErrType.errType(BuyCarActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initBuycarView() {
        carlist = (SlideListView1) findViewById(R.id.buycar_list);
        carback = (ImageView) findViewById(R.id.buycar_back);
        backmain = (ImageView) findViewById(R.id.buycar_backmain);
        backmain.setOnClickListener(this);
        carback.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buycar_back:
                finish();
                break;
            case R.id.buycar_backmain:
                startActivity(new Intent(BuyCarActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;

        }
    }
    // 加入购物车
    private void addCar(String id, final int p) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.ADDCARSERVICE);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",token);
        params.addHeader("appId",appId);
        params.addHeader("userId",userId);
        JSONObject object = new JSONObject();
        object.put("serviceId",id);
        params.setBodyContent(object.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                adapter.list.remove(p);
                adapter.notifyDataSetChanged();
                try {
                    carlist.turnToNormal();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                carlist.invalidate();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
